package com.example.chatapp.service;

import com.example.chatapp.manager.RedisCacheManager;
import org.springframework.stereotype.Service;

/**
 * 序列号服务
 * 职责：管理消息序列号生成
 */
@Service
public class SeqService {
    private final RedisCacheManager cacheManager;

    public SeqService(RedisCacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    /**
     * 获取下一个 seq（原子递增）
     */
    public long nextSeq(String roomId) {
        long seq = cacheManager.getNextSeq(roomId);
        System.out.println("[SeqService] Generated seq=" + seq + " for room: " + roomId);
        return seq;
    }

    /**
     * 获取当前 seq（nullable）
     */
    public Long currentSeq(String roomId) {
        Long currentSeq = cacheManager.getCurrentSeq(roomId);
        System.out.println("[SeqService] Current seq=" + currentSeq + " for room: " + roomId);
        return currentSeq;
    }
}
